﻿using ahdelivery.user;
using ahdelivery.util;
using CCWin;
using Center.core;
using Center.lib.utils.common;
using Center.lib.utils.http;
using Center.lib.utils.json;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace PrinterApplication.user
{
    public partial class LoginFrm : UIForm
    {
        public LoginFrm()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            string path = Application.StartupPath;
            label2.BackColor = Color.Transparent;
            label2.ForeColor = Color.White;
            label3.BackColor = Color.Transparent;
            label3.ForeColor = Color.White;
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }


        private void LoginFrm_Load(object sender, EventArgs e)
        {
            this.FormBorderStyle = FormBorderStyle.None;
            String user = JsonFileUtils.Readjson("user.json");
            if (!String.IsNullOrEmpty(user))
            {
                LoginUser loginuser= JsonHelper.FromJson<LoginUser>(user);
                this.txtPwd.Text = loginuser.password;
                this.txtUser.Text = loginuser.username;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
        }

        private void skinButton1_Click(object sender, EventArgs e)
        {
            Login();
        }
        private async void Login()
        {
            try
            {
                if (!StringUtil.NotBlank(this.txtUser.Text) || !StringUtil.NotBlank(this.txtPwd.Text)) {
                    UIMessageBox.ShowError(@"请输入用户名和密码");
                } else {
                    PrintUserDTO user = new PrintUserDTO();
                    user.username = this.txtUser.Text;
                    user.password = this.txtPwd.Text;
                    
                    JsonFileUtils.WriteJson("user.json", JsonHelper.ToJson(user));

                    user.password = util.Md5Utils.getMd5(util.Md5Utils.getMd5(this.txtPwd.Text) + this.txtUser.Text).ToUpper();
                    String result = await HttpUtils.PostData(util.Config.URI_LOGIN, user);
                    RestModel<PrintUserDTO> rest = JsonHelper.FromJson<RestModel<PrintUserDTO>>(result);
                    if (rest.code == 200)
                    {
                        util.Config.g_user = rest.data;
                        //todo：需要返回快递名称；
                      //  util.Config.g_user.deliveryTypeName = util.Config.g_user.userName;
                      //  result = await HttpUtils.PostData(String.Format("{0}/{1}", util.Config.URI_FETCH_PRODUCTS, util.Config.g_user.deliveryTypeId));
                      //  RestModel<List<String>> products = JsonHelper.FromJson<RestModel<List<String>>>(result);
                      //  util.Config.g_user.commodityCodes = products.data;
                        this.Hide();
                        new CenterFrm().ShowDialog();
                    }
                    else
                    {
                        UIMessageBox.ShowError(rest.msg);
                       // UIMessageBox.ShowError(@"用户名和密码错误");
                    }
                }
            }
            catch(Exception e)
            {
                UIMessageBox.ShowError("系统异常,请联系管理员", true);
            }
        }

        private void skinButton2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(@"确定退出？", @"提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
            {
                Application.Exit();
            }
        }

        private void LoginFrm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Login();
            }
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            Login();
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(@"确定退出？", @"提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
            {
                Application.Exit();
            }
        }
    }
}
